package com.work.music.utils;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONArray;
import org.json.JSONObject;

public class ImgWord {
    //设置APPID/AK/SK
    public static final String APP_ID = "10746721";
    public static final String API_KEY = "zTxRltK5bLGGk9Z5iiX1OY1v";
    public static final String SECRET_KEY = "wtO03eIjpD8nBlgLlvHwWZ9Wt7qZLPWd";
    public static AipOcr client;

    static {
        // 初始化一个AipOcr
        client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
    }

    public static String word(String path) {
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        JSONObject jsonObject = client.accurateGeneral(path, null);
        System.out.println(jsonObject);
        String result = "";
        JSONArray jsonArray = jsonObject.getJSONArray("words_result");

        if(jsonArray != null && jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i ++) {
                JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                result += jsonObject1.getString("words");
            }
        } else
            return "";
        return result;
    }
}
